package com.fintech.third.utils.shzx;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.marvels.common.utils.MarvelsLogUtil;

import net.sf.json.xml.XMLSerializer;

public class ShzxUtil {
	/**
	 * 解析返回报文为xml
	 * 
	 * @param result
	 * @return
	 */
	public static String convertSoapToXML(String result) {

		result = StringUtils.remove(result, "\\");
		result = StringEscapeUtils.unescapeXml(result);
		MarvelsLogUtil.getInstance().info("=============>>>>>unescapeXml:"+result);
		result = StringUtils.substringBetween(result, "<?xml", ">\"");
		MarvelsLogUtil.getInstance().info("=============>>>>>substringBetween<?xml:"+result);
		if(result==null || result==""){
			result="";
		}else{
			result = "<?xml" + result + ">";
		}

		return result;
	}
	
	
	public static String splitSoapToReturn(String result) {

		result = StringUtils.remove(result, "\\");
		result = StringEscapeUtils.unescapeXml(result);
		MarvelsLogUtil.getInstance().info("=============>>>>>unescapeXml:"+result);
		result = StringUtils.substringBetween(result, "[","]");
		
		MarvelsLogUtil.getInstance().info("=============>>>>>substringBetween[]:"+result);

		return result;
	}


	/**
	 * xml转map
	 * 
	 * @param xml
	 * @return
	 */
	public Map xmltoMap(String xml) {
		try {
			Map map = new HashMap();
			Document document = DocumentHelper.parseText(xml);
			map=Dom2Map(document.getRootElement());
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private Map Dom2Map(Element e) {   
	      Map map = new HashMap();    
	      List list = e.elements();   
	      if (list.size() > 0) {   
	          for (int i = 0; i < list.size(); i++) {   
	              Element iter = (Element) list.get(i);   
	              List mapList = new ArrayList();   
	   
	             if (iter.elements().size() > 0) {   
	                  Map m = Dom2Map(iter);   
	                  if (map.get(iter.getName()) != null) {   
	                      Object obj = map.get(iter.getName());   
	                      if (!obj.getClass().getName().equals(   
	                              "java.util.ArrayList")) {   
	                          mapList = new ArrayList();   
	                          mapList.add(obj);   
	                         mapList.add(m);   
	                      }   
	                      if (obj.getClass().getName().equals(   
	                             "java.util.ArrayList")) {   
	                          mapList = (List) obj;   
	                          mapList.add(m);   
	                      }   
	                      map.put(iter.getName(), mapList);   
	                  } else  
	                      map.put(iter.getName(), m);   
	              } else {   
	                  if (map.get(iter.getName()) != null) {   
	                      Object obj = map.get(iter.getName());   
	                      if (!obj.getClass().getName().equals(   
	                              "java.util.ArrayList")) {   
	                          mapList = new ArrayList();   
	                          mapList.add(obj);   
	                          mapList.add(iter.getText());   
	                     }   
	                      if (obj.getClass().getName().equals(   
	                              "java.util.ArrayList")) {   
	                          mapList = (List) obj;   
	                          mapList.add(iter.getText());   
	                      }   
	                      map.put(iter.getName(), mapList);   
	                  } else  
	                     map.put(iter.getName(), iter.getText());   
	              }   
	          }   
	      } else  
	          map.put(e.getName(), e.getText());   
	      return map;   
	  } 
	
	
	
	
	 /** 
     * 从输入流中获取字节数组 
     * @param inputStream 
     * @return 
     * @throws IOException 
     */  
    public static  byte[] readInputStream(InputStream inputStream) throws IOException {    
        byte[] buffer = new byte[1024];    
        int len = 0;    
        ByteArrayOutputStream bos = new ByteArrayOutputStream();    
        while((len = inputStream.read(buffer)) != -1) {    
            bos.write(buffer, 0, len);    
        }    
        bos.close();    
        return bos.toByteArray();    
    }    
  
    public static String xmltoJson(String xml) {  
        XMLSerializer xmlSerializer = new XMLSerializer();  
        return xmlSerializer.read(xml).toString();  
    } 
    
    
 
}
